package com.gillion.grule.client.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.io.Serializable;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(isGetterVisibility = JsonAutoDetect.Visibility.NONE)
public class Response implements Serializable {
    private static final long serialVersionUID = -6379822256603277872L;

    private ResponseHeader header;

    private Object body;

    @JsonIgnore
    public int getStatusCode() {
        if (this.header == null)
            return 5000000;
        return this.header.getReturnCode();
    }

    @JsonIgnore
    public <R> R getBody(Class t) {
        if (this.body != null) {
            Object obj = getBody();
            if (obj instanceof List) {
                List<Object> results = EntityUtils.list2Beans((List)obj, t);
                return (R)results;
            }
            Object result = EntityUtils.object2Bean(this.body, t);
            return (R)result;
        }
        return null;
    }

    public Object getBody() {
        return this.body;
    }

    public <T> void parseBody(Class<T> clazz) {
        if (this.body != null) {
            T t = (T)EntityUtils.object2Bean(this.body, clazz);
            this.body = t;
        }
    }

    public ResponseHeader getHeader() {
        return this.header;
    }

    public void setHeader(ResponseHeader header) {
        this.header = header;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}